home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_111 / assigndev / fprints.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  83 lines

  1. /* -----------------------------------------------------:ts=8-------------
  2.  *
  3.  * FPRINTS
  4.  *
  5.  * A simplified version of fprintf, which only supports %s format.
  6.  *
  7.  * Usage: int count = fprints(BPTR struct FileHandle, char *format,
  8.  *                            char *argument, ... );
  9.  * The format string consists of characters which are output, except for
  10.  * the combination `%s' which is replaced by the string pointed to by
  11.  * the corresponding `argument'.
  12.  * A non-recognized character following the % is output literally, so
  13.  * you may use %% to output a single percent sign.
  14.  * The returned value `count' is the number of characters that should
  15.  * have been written, or 0 if an error has been detected.
  16.  * Note that errors are NOT detected.
  17.  *
  18.  * ----------------------------------------------------------------------- */
  19.  
  20. #define BUFSIZE        80
  21. #define OVERKILL    0
  22.  
  23. #define PutChar(b,c)        {(b).Buf[(b).Count++]=c;}
  24. #define MayFlushBuffer(b)    {if((b).Count>=BUFSIZE) FlushBuffer(b);}
  25. #define FlushBuffer(b)\
  26.         {Write((b).Fh, &(b).Buf, (long)(b).Count); (b).Count=0;}
  27.  
  28. typedef struct {
  29.     unsigned short    Count;              /* Number of chars in the buffer */
  30.     long        Fh;          /* FileHandle assiciated with it */
  31.     char        Buf[BUFSIZE+OVERKILL];
  32. } BUFFER;
  33.  
  34. static BUFFER Buffer;                     /* NOT re-entrant */
  35.  
  36. /*VARARGS*/             /* Does anyone know a nice sweet lint for me? */
  37.  
  38. int fprints(file, format, arguments)
  39. long file;
  40. register char *format;
  41. char *arguments;
  42. {
  43.     register char **argptr = &arguments;
  44.     register char *argchar;
  45.     register char ch;
  46.     int total = 0;
  47.  
  48.     Buffer.Fh = file;
  49.     Buffer.Count = 0;
  50.  
  51.     while (ch = *format++) {     /* Until the end of the format string */
  52.         if ((ch == '%') && ((ch = *format++) == 's')) {
  53.             argchar = *argptr++;
  54.             while (ch = *argchar++) {     /* Copy the argument */
  55.                 PutChar(Buffer, ch);
  56.                 MayFlushBuffer(Buffer);
  57.                 total++;
  58.             }
  59.             continue;
  60.         } else {      /* A normal character: copy it to the buffer */
  61.             PutChar(Buffer, ch);
  62.             MayFlushBuffer(Buffer);
  63.             total++;
  64.         }
  65.     }                    /* End while format string */
  66.  
  67.     if (Buffer.Count)
  68.         FlushBuffer(Buffer);   /* Write out the rest of the buffer */
  69.  
  70.     return total;
  71. }
  72.  
  73. #if 0                         /* Currently not used anymore */
  74.  
  75. int fputs(file, string)
  76. long file;
  77. char *string;
  78. {
  79.     return Write(file, string, strlen(string));
  80. }
  81.  
  82. #endif
  83.